#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@Author   : long.hai
@Contact  : 1982964634@qq.com
"""
from _base.base import BasePage


class Judge:
    """
    步骤判断逻辑处理
    """
    @staticmethod
    async def compare_two_value(*args, **kwargs):
        """
        判断两个值满足指定条件
        :param args:
        :param kwargs: left：参数1  condition：判断条件  right：参数2
        :return:
        """
        value1, condition, value2 = kwargs["left"], kwargs["condition"], kwargs["right"]
        # print(f"判断两个值满足指定条件: left：{value1}  condition：{condition}  right：{value2}")
        if condition == "equal":
            return value1 == value2
        elif condition == "not_equal":
            return value1 != value2
        elif condition == "greater_than":
            return value1 > value2
        elif condition == "greater_or_equals":
            return value1 >= value2
        elif condition == "less_than":
            return value1 < value2
        elif condition == "less_or_equals":
            return value1 <= value2
        elif condition == "contains":
            return value1 in value2
        elif condition == "not_contains":
            return value1 not in value2
        else:
            raise ValueError("无效的比较符号")

    @staticmethod
    async def element_exist(base_page: BasePage, **kwargs):
        """
        判断元素是否存在
        :param base_page:
        :param kwargs:
        :return:
        """
        params = {"locators": [{"by": kwargs["by"], "params": kwargs["locator"]}]}
        ele_is_enabled = await base_page.assert_element_exist(**params)
        return ele_is_enabled

    @staticmethod
    async def element_attr_exist(base_page: BasePage, **kwargs):
        """
        判断元素属性是否存在
        :param base_page:
        :param kwargs:
        :return:
        """
        kwargs = {"locators": [{"by": kwargs["by"], "params": kwargs["locator"]}], "att_name": kwargs["attr"]}
        return await base_page.get_attr_exist(**kwargs)

    @staticmethod
    async def element_attr_value(base_page: BasePage, **kwargs):
        """
        判断元素属性值
        :param base_page:
        :param kwargs:
        :return:
        """
        params = {"locators": [{"by": kwargs["by"], "params": kwargs["locator"]}], "att_name": kwargs["attr"]}
        ele_attr_val = await base_page.get_attr_val(**params)
        if_params = {"left": ele_attr_val, "condition": kwargs["condition"], "right": kwargs["attr_val"]}
        return await Judge.compare_two_value(**if_params)

    @staticmethod
    async def element_num(base_page: BasePage, **kwargs):
        """
        判断元素个数
        :param base_page:
        :param kwargs:
        :return:
        """
        ele_num = await base_page.get_element_numbers(**kwargs)
        if_params = {"left": ele_num, "condition": kwargs["condition"], "right": int(kwargs["ele_num"])}
        return await Judge.compare_two_value(**if_params)
